package theOrbalEra.client.OrbalArts.entity;

import java.util.List;

import theOrbalEra.common.util.OrbalArtsDamageSource;
import net.minecraft.entity.Entity;
import net.minecraft.entity.EntityLiving;
import net.minecraft.entity.player.EntityPlayer;
import net.minecraft.nbt.NBTTagCompound;
import net.minecraft.util.AxisAlignedBB;
import net.minecraft.util.MathHelper;
import net.minecraft.util.MovingObjectPosition;
import net.minecraft.util.Vec3;
import net.minecraft.world.World;

public class OrbalArtEntityFireBolt extends OrbalArtEntityToOneShoot  {

	public OrbalArtEntityFireBolt(World world,EntityPlayer player, EntityLiving living,double par3, double par5, double par7) {
		super(world,player, living, par3, par5, par7);
		this.setSize(0.5F, 0.5F);
		this.scaleOfAcceleration=0.2;
		this.setFire(10000);

	}
	

	
	public OrbalArtEntityFireBolt(World world, EntityPlayer player) {
		super(world, player);
		this.setSize(0.5F, 0.5F);
		this.scaleOfAcceleration=0.2;
	}



	@Override
	protected void onImpact(MovingObjectPosition movingobjectposition) {
	

        if (!this.worldObj.isRemote)
        {
            if (movingobjectposition.entityHit != null)
            {movingobjectposition.entityHit.setFire(40);
                movingobjectposition.entityHit.attackEntityFrom(OrbalArtsDamageSource.causeOrbalArtsDamage(player), 6);
            
            }
            this.setDead();
        }
		 

	}


	@Override
	public void onUpdate() {
        Vec3 vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
        Vec3 vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
        MovingObjectPosition movingobjectposition = this.worldObj.rayTraceBlocks(vec3, vec31);
        vec3 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX, this.posY, this.posZ);
        vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(this.posX + this.motionX, this.posY + this.motionY, this.posZ + this.motionZ);
        if (movingobjectposition != null)
        {
            vec31 = this.worldObj.getWorldVec3Pool().getVecFromPool(movingobjectposition.hitVec.xCoord, movingobjectposition.hitVec.yCoord, movingobjectposition.hitVec.zCoord);
        }

        Entity entity = null;
        List list = this.worldObj.getEntitiesWithinAABBExcludingEntity(this,  AxisAlignedBB.getBoundingBox(this.posX-this.boundingBox.minX, this.posY-this.boundingBox.minY, this.posZ-this.boundingBox.minZ, this.posX+this.boundingBox.minX, this.posY+this.boundingBox.minY, this.posZ+this.boundingBox.minZ));
        
        for (int j = 0; j < list.size(); ++j)
        {
            Entity entity1 = (Entity)list.get(j);

            if (entity1.canBeCollidedWith() && (entity1.isEntityEqual(this.target) || this.ticksLived >= 25))
            {
            	entity = entity1;
              
            }
        }

        if (entity != null)
        {
            movingobjectposition = new MovingObjectPosition(entity);
        }

        if (movingobjectposition != null&&entity!=null&&this.getDistanceToEntity(entity)<=1.0)
        {
            this.onImpact(movingobjectposition);
        }

        this.posX += this.motionX;
        this.posY += this.motionY;
        this.posZ += this.motionZ;
        float f1 = MathHelper.sqrt_double(this.motionX * this.motionX + this.motionZ * this.motionZ);
        this.rotationYaw = (float)(Math.atan2(this.motionZ, this.motionX) * 180.0D / Math.PI) + 90.0F;

        for (this.rotationPitch = (float)(Math.atan2((double)f1, this.motionY) * 180.0D / Math.PI) - 90.0F; this.rotationPitch - this.prevRotationPitch < -180.0F; this.prevRotationPitch -= 360.0F)
        {
            ;
        }

        while (this.rotationPitch - this.prevRotationPitch >= 180.0F)
        {
            this.prevRotationPitch += 360.0F;
        }

        while (this.rotationYaw - this.prevRotationYaw < -180.0F)
        {
            this.prevRotationYaw -= 360.0F;
        }

        while (this.rotationYaw - this.prevRotationYaw >= 180.0F)
        {
            this.prevRotationYaw += 360.0F;
        }

        this.rotationPitch = this.prevRotationPitch + (this.rotationPitch - this.prevRotationPitch) * 0.2F;
        this.rotationYaw = this.prevRotationYaw + (this.rotationYaw - this.prevRotationYaw) * 0.2F;
        float f2 =  0.95F;
        this.motionX += this.accelerationX;
        this.motionY += this.accelerationY;
        this.motionZ += this.accelerationZ;
        this.motionX *= (double)f2;
        this.motionY *= (double)f2;
        this.motionZ *= (double)f2;
	}



	@Override
	protected void readEntityFromNBT(NBTTagCompound nbttagcompound) {
	}



	@Override
	protected void writeEntityToNBT(NBTTagCompound nbttagcompound) {
	}



	@Override
	protected void entityInit() {
	}

}
